/*
* @Author: slp
* @Date:   2018-07-05 16:26:25
* @Last Modified by:   slp
* @Last Modified time: 2018-07-06 10:13:37
*/

#include <sys/stat.h>

void uart_write(const char c)
{
	_pl01x_putc(c);
}

char uart_read()
{
	return _pl01x_getc();
}

int puts(const char *s)
{
	int n=0;
	for(;*s; s++, n++)
		uart_write(*s);
	return n;
}

void _exit(int status)
{
	while(1);
}

caddr_t _sbrk(int incr) 
{
	// static char *heap_end = 0;
	// extern char heap_low; /* defined by the linker*/
	// extern char heap_top; /* defined by the linker */
	// char *prev_heap_end;
	// if (heap_end == 0) {
	// 	heap_end = &heap_low;
	// }
	// prev_heap_end = heap_end;
	// if (heap_end+incr > &heap_top) {
	// 	return (caddr_t)0;
	// }
	// heap_end += incr;
	// return (caddr_t) prev_heap_end;
	return 0;
}
int _write(int file, char *ptr, int len) 
{
	int todo;
	for(todo = 0; todo < len; todo++)
		uart_write(*ptr++);
	return len;
}

int _close(int file) 
{ 
	return -1;
}
int _fstat(int file, struct stat *st)
{
	st->st_mode = S_IFCHR;
	return 0;
}

int _isatty(int file) 
{
	return 1;
}

int _lseek(int file, int ptr, int dir)
{
	return 0;
}

int _read(int file, char *ptr, int len)
{
	int todo;
	if(len == 0) return 0;
	for(todo = 1; todo<len; todo++){
		*ptr++ = uart_read();
	}
}

void initialise_monitor_handles()
{

}